import axios from 'axios'

const axiosInstance = axios.create({
  baseURL: '/api',
  timeout: 5000,
})

const token = '1V218k8Lf1s='

axiosInstance.interceptors.request.use(
  (config) => {
    if (config.method === 'get') {
      config.params = {
        ...config.params, // 保留原有参数
        token, // 添加 token
      }
    }
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

axiosInstance.interceptors.response.use(
  (response) => response,
  (error) => Promise.reject(error)
)

// 修改 get 方法，直接传递 params 而不是 { params }
const get = (url, params) => axiosInstance.get(url, { params })
const post = (url, data) => axiosInstance.post(url, data)
const put = (url, data) => axiosInstance.put(url, data)
const del = (url, data) => axiosInstance.delete(url, { data })

export default { get, post, put, del }
